<?php
// +----------------------------------------------------------------------
// | Diygw PHP
// +----------------------------------------------------------------------
// | Copyright (c) 2022~2024 https://www.diygw.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: diygw <diygwcom@diygw.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\shop\model;

use diygw\model\DiygwModel;

/**
 * @package app\shop\model
 */
class GoodsModel extends DiygwModel
{
    // 表名
    public $name = 'shop_goods';

    // 相似查询字段
    protected $likeField=[];

    public function afterAdd(&$data)
    {
        if($data['skuType']=='2'){
            $skuData = $data['skus'];
            $skus = $skuData['skus'];
            if(count($skus)>0){
                $minData = $skus[0];
                $amount = 0;
                foreach ($skus as $sku){
                    $amount = $amount + $sku['amount'];
                    $sku['goodsId'] = $data['id'];
                    //获取最小价格值
                    if($minData['price']>$sku['price']){
                        $minData = $sku;
                    }
                }
                $goodsModel = new GoodsModel();
                $goodsModel->where('id',$data['id'])->update(['price'=>$minData['price'],'line_price'=>$minData['price'],'amount'=>$amount]);
            }
        }
        return true;
    }

    public function beforeEdit(&$data)
    {
        if($data['skuType']=='2'){
            $skuData = $data['skus'];
            $skus = $skuData['skus'];
            if(count($skus)>0){
                $minData = $skus[0];
                $amount = 0;
                foreach ($skus as $sku){
                    $amount = $amount + $sku['amount'];
                    $sku['goodsId'] = $data['id'];
                    //获取最小价格值
                    if($minData['price']>$sku['price']){
                        $minData = $sku;
                    }
                }
                $data['price'] = $minData['price'];
                $data['linePrice'] = $minData['linePrice'];
                $data['amount'] = $amount;
            }
        }
        return true;
    }

    /**
     * @param $order
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function changeByOrder($order){
        $body = json_decode($order['body'],true);
        $carts = $body['carts'];
        foreach ($carts as $cart){
            $goodsModel = new GoodsModel();
            $goodsData = $goodsModel->where('id',$cart['id'])->find()->toArray();
            $number = $cart['number'];
            if(isset($cart['specIds'])){
                $skus = json_decode($goodsData['skus'],true);
                $skusDatas = $skus['skus'];
                foreach ($skusDatas as &$sku){
                    if($sku['specIds']==$cart['specIds']){
                        $sku['amount'] = $sku['amount'] - $number;
                    }
                }
                $goodsModel = new GoodsModel();
                $goodsModel->where('id',$cart['id'])->update(['sellamount'=>$goodsData['sellamount']+$number,'skus'=>json_encode($skus,true)]);
            }else{
                $goodsModel = new GoodsModel();
                $goodsModel->where('id',$cart['id'])->update(['sellamount'=>$goodsData['sellamount']+$number]);
            }
        }
    }
}
